﻿//*************************************
// JAVASCRIPT HELPES
//*************************************

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

Function.prototype.methodStatic = function (name, func) {
    this[name] = func;
    return this;
};

String.prototype.StartsWith = function (str) {
    return (this.match("^" + str) == str)
}


//*************************************
// UTILS
//*************************************

String.methodStatic("Format", function (str) {
    for (i = 1; i < arguments.length; i++) {
        str = str.replace('{' + (i - 1) + '}', arguments[i]);
    }
    return str;
});

String.methodStatic("InsertQuerystring", function (str, querystring) {
    str += (str.indexOf('?') == -1) ? '?$' + querystring + '=' : '&$' + querystring + '=';
    return str;
});


//*************************************
// ODATA OPERATIONS
//*************************************

String.method("SPSelect", function () {
    var str = String.InsertQuerystring(this, "select");
    for (i = 0; i < arguments.length; i++) {
        str += (i == 0) ? arguments[i] : ',' + arguments[i];
    }
    return str;
});

String.method("SPOrderBy", function (field, orderType) {
    var str = String.InsertQuerystring(this, "orderby");
    str += String.Format("{0} {1}", field, orderType);
    return str;
});

String.method("SPSkip", function (pageIndex) {
    var str = String.InsertQuerystring(this, "skip");
    str += pageIndex;
    return str;
});

String.method("SPTop", function (pageSize) {
    var str = String.InsertQuerystring(this, "top");
    str += pageSize;
    return str;
});

String.method("SPFilter", function (filter) {
    var str = String.InsertQuerystring(this, "filter");
    str += filter;
    return str;
});

String.method("SPFilterStartsWith", function (field, value) {
    if (value && value != '') {
        var str = String.InsertQuerystring(this, "filter");
        str += String.Format("startswith(" + field + ", '" + value + "')");
        return str;
    }
    return '';
});


//*************************************
// LISTS
//*************************************

String.method("SPGetListByGuid", function (guid) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists(guid\'{0}\')', guid);
    return str;
});

String.method("SPGetListGetByGuid", function (guid) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists/getbyid(guid\'{0}\')', guid);
    return str;
});

String.method("SPGetListByName", function (listName) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists/getbytitle(\'{0}\')', listName);
    return str;
});

String.method("SPItemsFromList", function (listName) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists/getbytitle(\'{0}\')/items', listName);
    return str;
});

String.method("SPItemGetByIdFromList", function (listName, id) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists/getbytitle(\'{0}\')/items/getbyid({1})', listName, id);
    return str;
});

String.method("SPItemByIdFromList", function (listName, id) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists/getbytitle(\'{0}\')/items({1})', listName, id);
    return str;
});

String.method("SPGetListIdByListName", function (listName) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists/getbytitle(\'{0}\')?$select=Id', listName);
    return str;
});

//*************************************
// DOCUMENT LIBRARIES
//*************************************

String.method("SPGetDocumentLibraryByRelativeUrl", function (relativeUrl) {
    var str = this;
    if (relativeUrl && relativeUrl != '') {
        if (!str.StartsWith('/'))
            str += '/';
        str += String.Format('_api/Web/GetFolderByServerRelativeUrl(\'{0}\')', relativeUrl);
        return str;
    }
    return str;
});

String.method("SPGetDocumentLibraryFilesByRelativeUrl", function (relativeUrl) {
    var str = this;
    if (relativeUrl && relativeUrl != '') {
        if (!str.StartsWith('/'))
            str += '/';
        str += String.Format('_api/Web/GetFolderByServerRelativeUrl(\'{0}\')/Files', relativeUrl);
        return str;
    }
    return str;
});

String.method("SPGetDocumentLibraryFilesByListTitle", function (title) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/lists/getbytitle(\'{0}\')/RootFolder/Files', title);
    return str;
});

String.method("SPAddFileToDomentLibrary", function (relativeUrl, fileName) {
    var str = this;
    if (relativeUrl && relativeUrl != '') {
        if (!str.StartsWith('/'))
            str += '/';
        str += String.Format('_api/Web/GetFolderByServerRelativeUrl(\'{0}\')/Files/add(url=\'{1}\')', relativeUrl, fileName);
        return str;
    }
    return str;
});

String.method("SPAddAndOverwriteFileToDomentLibrary", function (relativeUrl, fileName) {
    var str = this;
    if (relativeUrl && relativeUrl != '') {
        if (!str.StartsWith('/'))
            str += '/';
        str += String.Format('_api/Web/GetFolderByServerRelativeUrl(\'{0}\')/Files/add(url=\'{1}\', overwrite=true)', relativeUrl, fileName);
        return str;
    }
    return str;
});

//*************************************
// CONTEXT INFOS
//*************************************

String.method("SPContextInfo", function () {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    return String.Format("{0}_api/contextinfo", str);
});

String.method("SPWeb", function () {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += '_api/web';
    return str;
});

String.method("SPWebProperty", function (property) {
    var str = this;
    if (!str.StartsWith('/'))
        str += '/';
    str += String.Format('_api/web/{0}', property);
    return str;
});